package com.tt.controller;

import java.io.File;
import java.io.IOException;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import com.tt.entity.Food;
import com.tt.service.FoodserviceImpl;

/**
 * 
 * @author hqr
 * 2019.12.24 
 *
 */

@Controller
public class FoodController {
  @Resource
  private FoodserviceImpl  foodserviceImpl; 
   
  //��ѯ����
  @RequestMapping("foodfoundAll")
  public String foodfoundAll(Model model){
	 List<Food> foodlist = foodserviceImpl.foodfoundAll();
 	 //Model��ʽ���ݻ��ԣ������ݴ���jsp
	 model.addAttribute("foodlists",foodlist);
	  return "foodindex.jsp";
	 
  }
  
  
  //���
  @RequestMapping(value="addFood",method = RequestMethod.POST,produces = "text/plain;charset=UTF-8")
  public String addFood(Food food,@RequestParam("file1") MultipartFile file1,HttpServletRequest request)throws IllegalStateException, IOException{
	  System.out.println(food);
	  if(!file1.isEmpty()){			  
		  file1.transferTo(new File(request.getServletContext().getRealPath("img"),file1.getOriginalFilename()));
		  food.setImg(file1.getOriginalFilename());
	  }  
	  
	  boolean result =foodserviceImpl.addFood(food);	  
	  return "foodfoundAll";
	  }
  
  //ɾ��
  @RequestMapping("deteleFood")
  public String deteleFood(String id) {
	  int ids=Integer.parseInt(id);
      foodserviceImpl.deteleFood(ids);
	     return "foodfoundAll";
}
      
     /* if(b){
 		 return "<script> alert('��½�ɹ�����');window.location='foodfoundAll'</script>";
 	  }else{
 		 return "<script> alert('��½ʧ�ܣ���');window.location='foodfoundAll'</script>";
      }*/
 
	 // System.out.println(id);  }
  
  //�޸�
  @RequestMapping(value="updateFood",method = RequestMethod.POST,produces = "text/plain;charset=UTF-8")
  public  String updateFood(Food food,@RequestParam("file1") MultipartFile file1,HttpServletRequest request)throws IllegalStateException, IOException{
	  //����ListImg����
	  
	  
	  if(!file1.isEmpty()){			  
		  file1.transferTo(new File(request.getServletContext().getRealPath("img"),file1.getOriginalFilename()));
		  food.setImg(file1.getOriginalFilename());
	  }  
	  
	
	  foodserviceImpl.updateFood(food);
	  return "foodfoundAll";
  
  }
  
   //id ����
  @RequestMapping("showFood")
	 public String showFood(String id,Model model){
	  int ids=Integer.parseInt(id);	 
	  Food food=foodserviceImpl.showFood(ids);
		 model.addAttribute("food",food);		
		 return "updatefoodtest.jsp";
}
  
  //foodsingle
  @RequestMapping("showFoodsingle")
  public String showFoodsingle(String id,Model model){
	  int ids=Integer.parseInt(id);	     
	     Food food=foodserviceImpl.showFood(ids);
		 model.addAttribute("food",food);
		 return "foodsingle.jsp";
  }
  
  
   }
